package com.iot.manager.ui.views;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.View;

import com.iot.manager.R;

public class ScannerOverlay extends View {
    private int squareWidth;
    private int squareHeight;
    private int lineColor;
    private int lineSpeed;
    private int cornerColor;
    private float cornerWidth;
    private float cornerLength;
    private Paint linePaint;
    private Paint cornerPaint;
    private int scanLineTop;
    private boolean isFirstTime = true;

    public ScannerOverlay(Context context) {
        super(context);
        init(null);
    }

    public ScannerOverlay(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(attrs);
    }

    private void init(AttributeSet attrs) {
        // 获取自定义属性
        TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.ScannerOverlay);
        squareWidth = a.getDimensionPixelSize(R.styleable.ScannerOverlay_square_width, 250);
        squareHeight = a.getDimensionPixelSize(R.styleable.ScannerOverlay_square_height, 250);
        lineColor = a.getColor(R.styleable.ScannerOverlay_line_color, 0xFF00FF00);
        lineSpeed = a.getInteger(R.styleable.ScannerOverlay_line_speed, 5);
        cornerColor = a.getColor(R.styleable.ScannerOverlay_corner_color, 0xFF00FF00);
        cornerWidth = a.getDimension(R.styleable.ScannerOverlay_corner_width, 5);
        cornerLength = a.getDimension(R.styleable.ScannerOverlay_corner_length, 20);
        a.recycle();

        // 初始化画笔
        linePaint = new Paint();
        linePaint.setColor(lineColor);
        linePaint.setStrokeWidth(2);
        linePaint.setStyle(Paint.Style.FILL);

        cornerPaint = new Paint();
        cornerPaint.setColor(cornerColor);
        cornerPaint.setStrokeWidth(cornerWidth);
        cornerPaint.setStyle(Paint.Style.STROKE);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        // 计算扫描框的位置
        int centerX = getWidth() / 2;
        int centerY = getHeight() / 2;
        int left = centerX - squareWidth / 2;
        int top = centerY - squareHeight / 2;
        int right = centerX + squareWidth / 2;
        int bottom = centerY + squareHeight / 2;

        // 绘制半透明背景
        canvas.drawColor(0x99000000);

        // 清除扫描框区域的半透明背景
        Paint clearPaint = new Paint();
        clearPaint.setStyle(Paint.Style.FILL);
        clearPaint.setColor(0x00000000);
        clearPaint.setXfermode(new android.graphics.PorterDuffXfermode(android.graphics.PorterDuff.Mode.CLEAR));
        canvas.drawRect(left, top, right, bottom, clearPaint);

        // 绘制扫描线
        if (isFirstTime) {
            scanLineTop = top;
            isFirstTime = false;
        }
        if (scanLineTop >= bottom) {
            scanLineTop = top;
        } else {
            scanLineTop += lineSpeed;
        }
        canvas.drawRect(left, scanLineTop, right, scanLineTop + 2, linePaint);

        // 绘制四个角
        // 左上角
        canvas.drawLine(left, top, left + cornerLength, top, cornerPaint);
        canvas.drawLine(left, top, left, top + cornerLength, cornerPaint);
        // 右上角
        canvas.drawLine(right - cornerLength, top, right, top, cornerPaint);
        canvas.drawLine(right, top, right, top + cornerLength, cornerPaint);
        // 左下角
        canvas.drawLine(left, bottom - cornerLength, left, bottom, cornerPaint);
        canvas.drawLine(left, bottom, left + cornerLength, bottom, cornerPaint);
        // 右下角
        canvas.drawLine(right - cornerLength, bottom, right, bottom, cornerPaint);
        canvas.drawLine(right, bottom - cornerLength, right, bottom, cornerPaint);

        // 继续绘制动画
        postInvalidateDelayed(16);
    }
} 